package com.itic.market.mobile.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.RandomStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.qcode.QcodeUtils;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.mobile.jd.MallJdUtils;
import com.itic.market.mobile.sharevo.ImgWordVo;
import com.itic.market.mobile.sharevo.UrlVo;
import com.itic.market.system.marketuserproduct.persistence.model.MarketUserProduct;
import com.itic.market.system.marketuserproduct.persistence.model.MarketUserProductExample;
import com.itic.market.system.marketuserproduct.persistence.model.MarketUserProductExample.Criteria;
import com.itic.market.system.marketuserproduct.service.MarketUserProductService;
import com.itic.market.system.product.persistence.model.Product;
import com.itic.market.system.product.service.ProductService;
import com.itic.market.system.productjd.persistence.model.ProductJd;
import com.itic.market.system.productjd.service.ProductJdService;
import com.itic.market.system.user.persistence.model.MarketUser;
import com.itic.market.system.weixinbatch.persistence.model.WeixinBatch;
import com.itic.market.system.weixinbatch.persistence.model.WeixinBatchExample;
import com.itic.market.system.weixinbatch.persistence.model.WeixinBatchVo;
import com.itic.market.system.weixinbatch.persistence.model.WeixinVo;
import com.itic.market.system.weixinbatch.service.WeixinBatchService;


/**
 * 用户登入接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/product")
public class MarketProductInterface
     extends BaseMobileInterface {
	
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductJdService productJdService;
	
	@Autowired
	private MarketUserProductService marketUserProductService;
	
	@Autowired
	private WeixinBatchService weixinBatchService;

	
	
	/**
     * 立刻推广
     * @return
     */
    @RequestMapping(value="/preSendNew")
    @ResponseBody
    public Map<String,Object> preSendNew(HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				Product product = productService.get(productId);
    				if( null != product ){
    					Map<String, Object> dataMap = new HashMap<String,Object>();
    					String marketUserProductId = "";
    					//判断用户的商品有没有，有就返回存在的
    					MarketUserProductExample example = new MarketUserProductExample();
    					Criteria criteria = example.createCriteria();
    					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    					criteria.andUserIdEqualTo(user.getId());
    					criteria.andProductIdEqualTo(productId);
    					List<MarketUserProduct> list = marketUserProductService.list(example);
    					if( list.size() > 0 ){
    						MarketUserProduct marketUserProduct = list.get(0);
    						marketUserProductId = marketUserProduct.getId();
    						result = SUCCESS;
    						errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}else{
    						marketUserProductId = IdGen.uuid();
    						
        					MarketUserProduct marketUserProduct = new MarketUserProduct();
        					marketUserProduct.setId(marketUserProductId);
            				//关联字段
            				marketUserProduct.setUserId(user.getId());
            				marketUserProduct.setProductId(productId);
            				//业务字段
            				marketUserProduct.setCategoryId(product.getCategoryId());
            				marketUserProduct.setName(product.getName());
            				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
            				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
            				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setTotalSales(product.getTotalSales());
            				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
            				marketUserProduct.setProfit("0");
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setProductType("001");
            				result = marketUserProductService.add(marketUserProduct);
            				errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}
    					
    					//图文分享
    					
    					//图片分享
    					
    					//链接分享
    					String url = getWordByMarketUserId(productId,marketUserProductId,user.getId());
    					dataMap.put("preMoney", product.getMarketProfit() );//预计收益多少钱
    					//三种分享方式 
    					//图文分享
    					ImgWordVo imgWord = new ImgWordVo();
    					imgWord.setImage(product.getMainImageUrl());
    					imgWord.setWord(product.getName()+"\n" +
    							  "----------------------\n" + 
			                      "友惠价：" + product.getProductOrginPrice() + "元\n" +
					              "券后价：" + product.getProductPrice() + "元\n" +
					              "新人价：1元\n" +
			                      "----------------------\n" + url);
    					dataMap.put( "imageWord" , imgWord );
    					//图片海报分享
    					String imageUrl = "http://file.jhjvip.cn:8082/mall/mobile/image/mergeYun?spuId=" + product.getId() + "&marketUserProductId="+ marketUserProductId+"&yunId="+user.getId() ;
    					dataMap.put("image", imageUrl);
    					//链接分享
    					UrlVo urlVo = new UrlVo();
    					urlVo.setImgUrl(product.getMainImageUrl());
    					urlVo.setTitle("京选友惠邀请注册");
    					urlVo.setContent(user.getUserNickName()+"赠送了您1元新手礼包，邀请您成为TA的VIP用户。" );
    					urlVo.setUrl(url);
    					dataMap.put("url", urlVo);
    					
    					resultMap.put(DATA, dataMap );
    					
    					result = SUCCESS;
    					errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG; 
    					
    				}else{
    					result = FAIL;
    					errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
    					errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG; 
    				}
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE; 
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
	
	/**
     * 立刻推广
     * @return
     */
    @RequestMapping(value="/preSend")
    @ResponseBody
    public Map<String,Object> preSend(HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				Product product = productService.get(productId);
    				if( null != product ){
    					Map<String, Object> dataMap = new HashMap<String,Object>();
    					String marketUserProductId = "";
    					//判断用户的商品有没有，有就返回存在的
    					MarketUserProductExample example = new MarketUserProductExample();
    					Criteria criteria = example.createCriteria();
    					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    					criteria.andUserIdEqualTo(user.getId());
    					criteria.andProductIdEqualTo(productId);
    					List<MarketUserProduct> list = marketUserProductService.list(example);
    					if( list.size() > 0 ){
    						MarketUserProduct marketUserProduct = list.get(0);
    						marketUserProductId = marketUserProduct.getId();
    						result = SUCCESS;
    						errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}else{
    						marketUserProductId = IdGen.uuid();
    						
        					MarketUserProduct marketUserProduct = new MarketUserProduct();
        					marketUserProduct.setId(marketUserProductId);
            				//关联字段
            				marketUserProduct.setUserId(user.getId());
            				marketUserProduct.setProductId(productId);
            				//业务字段
            				marketUserProduct.setCategoryId(product.getCategoryId());
            				marketUserProduct.setName(product.getName());
            				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
            				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
            				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setTotalSales(product.getTotalSales());
            				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
            				marketUserProduct.setProfit("0");
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setProductType("001");
            				result = marketUserProductService.add(marketUserProduct);
            				errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}
    					
    					//图文分享
    					
    					//图片分享
    					
    					//链接分享
    					String url = getWordByMarketUserId(productId,marketUserProductId,user.getId());
    					dataMap.put("preMoney", product.getMarketProfit() );//预计收益多少钱
    					//三种分享方式 
    					//图文分享
    					ImgWordVo imgWord = new ImgWordVo();
    					imgWord.setImage(product.getMainImageUrl());
    					//if("1652b63144c208d33ce31731c40b9"==productId){
					/*	location.href='http://jxyh.huifakeji.com/yhj/vip_share.html';
					}else if("d637bcefb1a244f1b96e4ffc7b0e1a52"==productId){
						location.href='http://jxyh.huifakeji.com/yhj/vip_share.html';
					}else if("8811b3e724f34cb098d0c121f1cc7112"==productId){
						location.href='http://jxyh.huifakeji.com/yhj/vip_share.html';
					}else if("c9ff10f2b3534dff98385c553d800cd3"==productId){
						location.href='http://jxyh.huifakeji.com/yhj/vip_share.html';
					}*/
    					if("1652b63144c208d33ce31731c40b9".equals(productId) || "d637bcefb1a244f1b96e4ffc7b0e1a52".equals(productId) || "8811b3e724f34cb098d0c121f1cc7112".equals(productId) || "c9ff10f2b3534dff98385c553d800cd3".equals(productId)){
    						imgWord.setWord(product.getName()+"\n" +
      							  "----------------------\n" + 
      							  "新手价：1元\n" +	
  			                      "友惠价：" + product.getProductOrginPrice() + "元\n" +
  					              "券后价：" + product.getProductPrice() + "元\n" +
  			                      "----------------------\n" + url);
    					}else{
    						imgWord.setWord(product.getName()+"\n" +
      							  "----------------------\n" + 
  			                      "友惠价：" + product.getProductOrginPrice() + "元\n" +
  					              "券后价：" + product.getProductPrice() + "元\n" +
  			                      "----------------------\n" + url);
    					}
    					
    					dataMap.put( "imageWord" , imgWord );
    					//图片海报分享
    					String imageUrl = "http://file.jhjvip.cn:8082/mall/mobile/image/mergeYun?spuId=" + product.getId() + "&marketUserProductId="+ marketUserProductId+"&yunId="+user.getId() ;
    					dataMap.put("image", imageUrl);
    					//链接分享
    					UrlVo urlVo = new UrlVo();
    					urlVo.setImgUrl(product.getMainImageUrl());
    					if("1652b63144c208d33ce31731c40b9".equals(productId) || "d637bcefb1a244f1b96e4ffc7b0e1a52".equals(productId) || "8811b3e724f34cb098d0c121f1cc7112".equals(productId) || "c9ff10f2b3534dff98385c553d800cd3".equals(productId)){
    						urlVo.setTitle("京选友惠VIP一元超值秒杀");
        					urlVo.setContent(user.getUserNickName()+"邀请您参加京选友惠特价一元秒杀，成为京选的VIP会员，天天特价，天天省钱！" );	
    					}else{
    						urlVo.setTitle("【领券更便宜】"+product.getName());
        					urlVo.setContent("原价"+product.getProductOrginPrice()+"元,券后价"+product.getProductPrice()+"元,友惠商城，正品保证。" );
    					}
    					urlVo.setUrl(url);
    					dataMap.put("url", urlVo);
    					
    					resultMap.put(DATA, dataMap );
    					
    					result = SUCCESS;
    					errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG; 
    					
    				}else{
    					result = FAIL;
    					errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
    					errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG; 
    				}
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE; 
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
	
    
    /**
     * 立刻推广
     * @return
     */
    @RequestMapping(value="/erHeYi")
    @ResponseBody
    public Map<String,Object> erHeYi(HttpServletRequest request,HttpServletResponse response, String token, String productId,String type ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) && StringHelper.isNotBlank(type)  ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				if( "002".equals(type) ){
    					ProductJd product = productJdService.get(productId);
        				if( null != product ){
        					Map<String, Object> dataMap = new HashMap<String,Object>();
        					String marketUserProductId = "";
        					//判断用户的商品有没有，有就返回存在的
        					MarketUserProductExample example = new MarketUserProductExample();
        					Criteria criteria = example.createCriteria();
        					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        					criteria.andUserIdEqualTo(user.getId());
        					criteria.andProductIdEqualTo(productId);
        					List<MarketUserProduct> list = marketUserProductService.list(example);
        					if( list.size() > 0 ){
        						MarketUserProduct marketUserProduct = list.get(0);
        						marketUserProductId = marketUserProduct.getId();
        						result = SUCCESS;
        						errorCode = SUCCESS_ERROR_CODE;
                	        	errorMsg = SUCCESS_ERROR_MSG; 
        					}else{
        						marketUserProductId = IdGen.uuid();
        						
            					MarketUserProduct marketUserProduct = new MarketUserProduct();
            					marketUserProduct.setId(marketUserProductId);
                				//关联字段
                				marketUserProduct.setUserId(user.getId());
                				marketUserProduct.setProductId(productId);
                				//业务字段
                				marketUserProduct.setCategoryId(product.getCategoryName());
                				marketUserProduct.setName(product.getGoodsName());
                				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getOriPrice()+""));
                				marketUserProduct.setProductHongbao(Double.parseDouble(product.getConTicketPrice()));
                				marketUserProduct.setProductPrice(Double.parseDouble(product.getConPrice()));
                				marketUserProduct.setTotalSales(Integer.parseInt(product.getTotalSales()));
                				marketUserProduct.setMainImageUrl(product.getGoodsUrl());
                				marketUserProduct.setProfit("0");
                				marketUserProduct.setMarketProfit(product.getCommissionProfit());
                				marketUserProduct.setMarketProfitPercent(product.getCommission());
                				marketUserProduct.setProductType("002");
                				result = marketUserProductService.add(marketUserProduct);
                				errorCode = SUCCESS_ERROR_CODE;
                	        	errorMsg = SUCCESS_ERROR_MSG; 
        					}
        					
        					String pid = getTgwidByUserid(user.getId());
        					String url = getConUrl(pid, product.getSku(), product.getConUrlB());
        					if( StringHelper.isBlank(url) ){
        						url = getCommonUrl(pid, product.getSku());
        					}
        					
        					dataMap.put("url", url );
        					//String mainImageUrl = product.getMainImageUrl();
        					//String[] split = mainImageUrl.split("=");
        					
        					//dataMap.put("imgUrl", sysSettingService.getByKey("MARKET_HONGBAO_URL"));
        					//String title = sysSettingService.getByKey("SHARE_TITLE");
        					//dataMap.put("title", title==null?"友惠家商城":title);
        					//dataMap.put("title", product.getName());
        					dataMap.put("title", user.getUserNickName()+"发来了一个"+ product.getConTicketPrice() +"元红包券，赶快来领取吧");
        					dataMap.put("content", "我在京东商城里看到一件不错的商品，赶快来看看吧！" );
        					resultMap.put(DATA, dataMap );
        					result = SUCCESS;
        					errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
        				}else{
        					result = FAIL;
        					errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
        					errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG; 
        				}
    				}
    				if( "001".equals(type) ){
    					Product product = productService.get(productId);
        				if( null != product ){
        					Map<String, Object> dataMap = new HashMap<String,Object>();
        					String marketUserProductId = "";
        					//判断用户的商品有没有，有就返回存在的
        					MarketUserProductExample example = new MarketUserProductExample();
        					Criteria criteria = example.createCriteria();
        					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        					criteria.andUserIdEqualTo(user.getId());
        					criteria.andProductIdEqualTo(productId);
        					List<MarketUserProduct> list = marketUserProductService.list(example);
        					if( list.size() > 0 ){
        						MarketUserProduct marketUserProduct = list.get(0);
        						marketUserProductId = marketUserProduct.getId();
        						result = SUCCESS;
        						errorCode = SUCCESS_ERROR_CODE;
                	        	errorMsg = SUCCESS_ERROR_MSG; 
        					}else{
        						marketUserProductId = IdGen.uuid();
        						
            					MarketUserProduct marketUserProduct = new MarketUserProduct();
            					marketUserProduct.setId(marketUserProductId);
                				//关联字段
                				marketUserProduct.setUserId(user.getId());
                				marketUserProduct.setProductId(productId);
                				//业务字段
                				marketUserProduct.setCategoryId(product.getCategoryId());
                				marketUserProduct.setName(product.getName());
                				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
                				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
                				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
                				marketUserProduct.setMarketProfit(product.getMarketProfit());
                				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
                				marketUserProduct.setTotalSales(product.getTotalSales());
                				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
                				marketUserProduct.setProfit("0");
                				marketUserProduct.setMarketProfit(product.getMarketProfit());
                				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
                				marketUserProduct.setProductType("001");
                				result = marketUserProductService.add(marketUserProduct);
                				errorCode = SUCCESS_ERROR_CODE;
                	        	errorMsg = SUCCESS_ERROR_MSG; 
        					}
        					
        					dataMap.put("url", getWordByMarketUserId(productId,marketUserProductId,user.getId()) );
        					//String mainImageUrl = product.getMainImageUrl();
        					//String[] split = mainImageUrl.split("=");
        					
        					dataMap.put("imgUrl", sysSettingService.getByKey("MARKET_HONGBAO_URL"));
        					//String title = sysSettingService.getByKey("SHARE_TITLE");
        					//dataMap.put("title", title==null?"友惠家商城":title);
        					//dataMap.put("title", product.getName());
        					dataMap.put("title", user.getUserNickName()+"发来了一个"+ getHongbao(product) +"元红包券，赶快来领取吧");
        					dataMap.put("content", "我在友惠商城里看到一件不错的商品，赶快来看看吧！" );
        					resultMap.put(DATA, dataMap );
        					result = SUCCESS;
        					errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
        				}else{
        					result = FAIL;
        					errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
        					errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG; 
        				}
    				}
    				
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE; 
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
   
    /**
     * 立刻推广
     * @return
     */
    @RequestMapping(value="/send")
    @ResponseBody
    public Map<String,Object> send(HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				Product product = productService.get(productId);
    				if( null != product ){
    					Map<String, Object> dataMap = new HashMap<String,Object>();
    					String marketUserProductId = "";
    					//判断用户的商品有没有，有就返回存在的
    					MarketUserProductExample example = new MarketUserProductExample();
    					Criteria criteria = example.createCriteria();
    					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    					criteria.andUserIdEqualTo(user.getId());
    					criteria.andProductIdEqualTo(productId);
    					List<MarketUserProduct> list = marketUserProductService.list(example);
    					if( list.size() > 0 ){
    						MarketUserProduct marketUserProduct = list.get(0);
    						marketUserProductId = marketUserProduct.getId();
    						result = SUCCESS;
    						errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}else{
    						marketUserProductId = IdGen.uuid();
    						
        					MarketUserProduct marketUserProduct = new MarketUserProduct();
        					marketUserProduct.setId(marketUserProductId);
            				//关联字段
            				marketUserProduct.setUserId(user.getId());
            				marketUserProduct.setProductId(productId);
            				//业务字段
            				marketUserProduct.setCategoryId(product.getCategoryId());
            				marketUserProduct.setName(product.getName());
            				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
            				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
            				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setTotalSales(product.getTotalSales());
            				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
            				marketUserProduct.setProfit("0");
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setProductType("001");
            				result = marketUserProductService.add(marketUserProduct);
            				errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}
    					
    					dataMap.put("url", getWordByMarketUserId(productId,marketUserProductId,user.getId()) );
    					//String mainImageUrl = product.getMainImageUrl();
    					//String[] split = mainImageUrl.split("=");
    					
    					dataMap.put("imgUrl", sysSettingService.getByKey("MARKET_HONGBAO_URL"));
    					//String title = sysSettingService.getByKey("SHARE_TITLE");
    					//dataMap.put("title", title==null?"友惠家商城":title);
    					//dataMap.put("title", product.getName());
    					dataMap.put("title", user.getUserNickName()+"发来了一个"+ getHongbao(product) +"元红包券，赶快来领取吧");
    					dataMap.put("content", "我在友惠商城里看到一件不错的商品，赶快来看看吧！" );
    					resultMap.put(DATA, dataMap );
    					result = SUCCESS;
    					errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG; 
    				}else{
    					result = FAIL;
    					errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
    					errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG; 
    				}
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE; 
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 立刻推广
     * @return
     */
    @RequestMapping(value="/sendNew")
    @ResponseBody
    public Map<String,Object> sendNew(HttpServletRequest request,HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				Product product = productService.get(productId);
    				if( null != product ){
    					Map<String, Object> dataMap = new HashMap<String,Object>();
    					String marketUserProductId = "";
    					//判断用户的商品有没有，有就返回存在的
    					MarketUserProductExample example = new MarketUserProductExample();
    					Criteria criteria = example.createCriteria();
    					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    					criteria.andUserIdEqualTo(user.getId());
    					criteria.andProductIdEqualTo(productId);
    					List<MarketUserProduct> list = marketUserProductService.list(example);
    					if( list.size() > 0 ){
    						MarketUserProduct marketUserProduct = list.get(0);
    						marketUserProductId = marketUserProduct.getId();
    						result = SUCCESS;
    						errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}else{
    						marketUserProductId = IdGen.uuid();
    						
        					MarketUserProduct marketUserProduct = new MarketUserProduct();
        					marketUserProduct.setId(marketUserProductId);
            				//关联字段
            				marketUserProduct.setUserId(user.getId());
            				marketUserProduct.setProductId(productId);
            				//业务字段
            				marketUserProduct.setCategoryId(product.getCategoryId());
            				marketUserProduct.setName(product.getName());
            				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
            				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
            				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setTotalSales(product.getTotalSales());
            				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
            				marketUserProduct.setProfit("0");
            				marketUserProduct.setMarketProfit(product.getMarketProfit());
            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
            				marketUserProduct.setProductType("001");
            				result = marketUserProductService.add(marketUserProduct);
            				errorCode = SUCCESS_ERROR_CODE;
            	        	errorMsg = SUCCESS_ERROR_MSG; 
    					}
    					
    					String url = getWordByMarketUserId(productId,marketUserProductId,user.getId());
    					
    					dataMap.put("url", url );
    					//String mainImageUrl = product.getMainImageUrl();
    					//String[] split = mainImageUrl.split("=");
    					
    					dataMap.put("imgUrl", sysSettingService.getByKey("MARKET_HONGBAO_URL"));
    					//String title = sysSettingService.getByKey("SHARE_TITLE");
    					//dataMap.put("title", title==null?"友惠家商城":title);
    					//dataMap.put("title", product.getName());
    					dataMap.put("title", product.getName());
    					dataMap.put("content", product.getName()+"\n" +
    					                      "----------------------\n" + 
    					                      "友惠价：" + product.getProductOrginPrice() + "\n" +
    							              "券后价：" + product.getProductPrice() + "\n" +
    					                      "----------------------\n" + url  );
    					resultMap.put(DATA, dataMap );
    				}else{
    					result = FAIL;
    					errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
    					errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG; 
    				}
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE; 
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 微信群发
     * @return
     */
    @RequestMapping(value="/weixinBatch")
    @ResponseBody
    public Map<String,Object> weixinBatch( HttpServletRequest request,HttpServletResponse response ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		WeixinBatchExample example = new WeixinBatchExample();
			example.setOrderByClause("CREATE_TIME DESC");
			example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andTypeEqualTo("1");
			List<WeixinBatch> list = weixinBatchService.list(example);
			resultMap.put(DATA, list );
			result = SUCCESS;
			errorCode = 	SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 微信群发图片详情
     * @return
     */
    @RequestMapping(value="/weixinBatchDetail")
    @ResponseBody
    public Map<String,Object> weixinBatchDetail( HttpServletRequest request,HttpServletResponse response, String id ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		WeixinBatch weixinBatch = weixinBatchService.get(id);
    		List<WeixinBatchDetailVo> pros = new ArrayList<WeixinBatchDetailVo>();
    		if( null != weixinBatch ){
    			String productJdIds = weixinBatch.getProductJdIds();
    			
    			if( StringHelper.isNotBlank(productJdIds) ){
    				String[] splitJd = productJdIds.split(",");
    				for( int i=0; i<splitJd.length;i++ ){
    					ProductJd productJd = productJdService.get(splitJd[i]);
    					WeixinBatchDetailVo vo = new WeixinBatchDetailVo();
    					vo.setImg(productJd.getGoodsUrl());
    					vo.setType("jd");
    					pros.add(vo);
    				}
    			}
    			String productIds = weixinBatch.getProductIds();
    			if( StringHelper.isNotBlank(productIds) ){
    				String[] split = productIds.split(",");
    				for( int i=0; i<split.length;i++ ){
    					Product product = productService.get(split[i]);
    					WeixinBatchDetailVo vo = new WeixinBatchDetailVo();
    					vo.setImg(product.getMainImageUrl());
    					vo.setType("yh");
    					pros.add(vo);
    				}
    			}
    		}
			resultMap.put(DATA, pros );
			result = SUCCESS;
			errorCode = 	SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }

    /**
     * 朋友 圈群发
     * @return
     */
    @RequestMapping(value="/friendBatch")
    @ResponseBody
    public Map<String,Object> friendBatch( HttpServletRequest request,HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				List<WeixinBatchVo> vos = new ArrayList<WeixinBatchVo>();
    				WeixinBatchExample example = new WeixinBatchExample();
    				example.setOrderByClause("CREATE_TIME DESC");
    				example=this.handlePageQuery(request, example);
    				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andTypeEqualTo("2");
    				List<WeixinBatch> list = weixinBatchService.list(example);
    				int count = weixinBatchService.count(example);
    				if( list.size() > 0 ){
    					for( WeixinBatch tmp : list ){
    						WeixinBatchVo vo = new WeixinBatchVo();
    						List<String> urls = new ArrayList<String>();
    						
    						if( StringHelper.isNotBlank(tmp.getProductIds()) ){
    							String[] split = tmp.getProductIds().split(",");
        						for( int i=0;i<split.length;i++ ){
        							Product product = productService.get(split[i]);
        							if( null != product ){
        								urls.add(product.getMainImageUrl());
        							}
        						}
    						}
    						
    						if( StringHelper.isNotBlank(tmp.getProductJdIds()) ){
    							String[] split = tmp.getProductJdIds().split(",");
        						//vo.setWord(tmp.getSayContent());
        						for( int i=0;i<split.length;i++ ){
        							ProductJd productJd = productJdService.get(split[i]);
        							if( null != productJd ){
        								urls.add(productJd.getGoodsUrl());
        							}
        						}
    						}
    						vo.setUrls(urls);
    						vo.setId(tmp.getId());
    						vo.setWord(tmp.getSayContent());
    						if(StringHelper.isBlank(user.getUserHeadImg())){
    							user.setUserHeadImg("http://file.jhjvip.cn/images/default.jpg");
    						}
    						vo.setHeadImg(user.getUserHeadImg());
    						vo.setName(user.getUserNickName());
    						vos.add(vo);
    					}
    				}
    				
    				dataMap.put( "totalCount", count );
    				dataMap.put( "product", vos );
    				resultMap.put(DATA,  dataMap);
    				
    				result = SUCCESS;
    				errorCode = 	SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    				
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 立刻推广多件
     * @return
     */
    @RequestMapping(value="/weixinBatchSend")
    @ResponseBody
    public Map<String,Object> weixinBatchSend( HttpServletRequest request,HttpServletResponse response, String token ,String id ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(id) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				WeixinBatch weixinBatch = weixinBatchService.get(id);
    				
    				if( null != weixinBatch ){
    					List<MarketUserProduct> userProducts = new ArrayList<MarketUserProduct>();
        				List<String> list = new ArrayList<String>();
        				String word="";
        				WeixinVo batch = new WeixinVo();
        				
        				String productIds = weixinBatch.getProductIds();
    					
    					if( StringHelper.isNotBlank(productIds) ){
        					String[] split = productIds.split(",");
            				
            				if( split.length>0 ){
            					for( int i=0; i<split.length; i++ ){
                					Product product = productService.get(split[i]);
                					if( null != product ){
                						//WeixinVo batch = new WeixinVo();
                    					//判断用户的商品有没有，有就返回存在的
                    					MarketUserProductExample example = new MarketUserProductExample();
                    					Criteria criteria = example.createCriteria();
                    					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
                    					criteria.andUserIdEqualTo(user.getId());
                    					criteria.andProductIdEqualTo(split[i]);
                    					List<MarketUserProduct> ttmp = marketUserProductService.list(example);
                    					String url = "http://file.jhjvip.cn:8082/mall/mobile/image/mergeYun?spuId=";
                    					if( ttmp.size() > 0 ){
                    						
                    						MarketUserProduct marketUserProduct = ttmp.get(0);
                    						list.add( url + product.getId() + "&marketUserProductId="+ marketUserProduct.getId()+"&yunId="+user.getId() );
                    						//batch.setImageUrl(product.getMainImageUrl());
                    						//batch.setWord(getWordByMarketUserId(marketUserProduct.getProductId(),marketUserProduct.getId()));
                    						//word += getWord(product.getName(),product.getProductHongbao(),product.getProduct Price(),marketUserProduct.getProductId(),marketUserProduct.getId());
                    					}else{
                    						
                    						String marketUserProductId = IdGen.uuid();
                    						
                        					MarketUserProduct marketUserProduct = new MarketUserProduct();
                        					marketUserProduct.setId(marketUserProductId);
                        					marketUserProduct.setOrgCode(ORG_CODE);
                        					marketUserProduct.setCreateTime(new Date());
                        					marketUserProduct.setUpdateTime(new Date());
                        					marketUserProduct.setIsEffective(DBConst.TRUE);
                        					marketUserProduct.setCreateUser(CREATE_USER);
                            				//关联字段
                            				marketUserProduct.setUserId(user.getId());
                            				marketUserProduct.setProductId(product.getId());
                            				marketUserProduct.setCategoryId(product.getCategoryId());
                            				marketUserProduct.setName(product.getName());
                            				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
                            				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
                            				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
                            				marketUserProduct.setMarketProfit(product.getMarketProfit());
                            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
                            				marketUserProduct.setTotalSales(product.getTotalSales());
                            				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
                            				marketUserProduct.setProfit("0");
                            				marketUserProduct.setMarketProfit(product.getMarketProfit());
                            				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
                            				marketUserProduct.setProductType("001");
                            				//batch.setImageUrl(product.getMainImageUrl());
                    						//batch.setWord(getWordByMarketUserId(marketUserProduct.getProductId(),marketUserProduct.getId()));
                            				//list.add(product.getMainImageUrl());
                            				//word += getWord(product.getName(),product.getProductHongbao(),product.getProductPrice(),marketUserProduct.getProductId(),marketUserProduct.getId());
                    						userProducts.add(marketUserProduct);
                    						list.add( url + product.getId() + "&marketUserProductId="+ marketUserProduct.getId() + "&yunId=" + user.getId() ); 
                    					}
                					}
                				}
            				}
        				}
        				
        				
        				
        				String productJdIds = weixinBatch.getProductJdIds();
        				if( StringHelper.isNotBlank(productJdIds) ){
        					String[] jdss = productJdIds.split(",");
            				if( jdss.length>0 ){
            					for( int i=0; i<jdss.length; i++ ){
                					ProductJd productJd = productJdService.get(jdss[i]);
                					//判断能否生成二合一链接
                					
                					if( null != productJd  ){
                						
                						String pid = getTgwidByUserid(user.getId());
            	    					String convert = getConUrl(pid, productJd.getSku(), productJd.getConUrlB());
            	    					if( StringHelper.isBlank(convert) ){
            	    						convert = getCommonUrl(pid, productJd.getSku());
            	    					}
                						
                						//jdCountStatics("0", convert, productJd.getSku(), "判断能否生成二合一链接");
                						if( null != convert ){
                							//WeixinVo batch = new WeixinVo();
                        					//判断用户的商品有没有，有就返回存在的
                        					MarketUserProductExample example = new MarketUserProductExample();
                        					Criteria criteria = example.createCriteria();
                        					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
                        					criteria.andUserIdEqualTo(user.getId());
                        					criteria.andProductIdEqualTo(jdss[i]);
                        					List<MarketUserProduct> ttmp = marketUserProductService.list(example);
                        					//String url = "http://file.jhjvip.cn:8082/mall/mobile/image/mergeJd?spuId=";
                        					if( ttmp.size() > 0 ){
                        						//String qcUrl = getConUrl(getTgwidByUserid(user.getId()), productJd.getSku(), productJd.getConUrlB());
                        						String imageUrl = getPoster(productJd.getGoodsName(), productJd.getGoodsUrl(), productJd.getOriPrice(), productJd.getConPrice(), productJd.getConTicketPrice() , productJd.getTotalSales(), "jd", convert);
                        						list.add( imageUrl );
                        						//MarketUserProduct marketUserProduct = ttmp.get(0);
                        						//list.add( url + productJd.getId() + "&marketUserProductId="+ marketUserProduct.getId()+"&yunId="+user.getId() );
                        						//batch.setImageUrl(product.getMainImageUrl());
                        						//batch.setWord(getWordByMarketUserId(marketUserProduct.getProductId(),marketUserProduct.getId()));
                        						//word += getWord(product.getName(),product.getProductHongbao(),product.getProduct Price(),marketUserProduct.getProductId(),marketUserProduct.getId());
                        					}else{
                        						
                        						String marketUserProductId = IdGen.uuid();
                        						
                            					MarketUserProduct marketUserProduct = new MarketUserProduct();
                            					marketUserProduct.setId(marketUserProductId);
                            					marketUserProduct.setOrgCode(ORG_CODE);
                            					marketUserProduct.setCreateTime(new Date());
                            					marketUserProduct.setUpdateTime(new Date());
                            					marketUserProduct.setIsEffective(DBConst.TRUE);
                            					marketUserProduct.setCreateUser(CREATE_USER);
                                				//关联字段
                                				marketUserProduct.setUserId(user.getId());
                                				marketUserProduct.setProductId(productJd.getId());
                                				//业务字段
                                				marketUserProduct.setCategoryId(productJd.getCategoryName());
                                				marketUserProduct.setName(productJd.getGoodsName());
                                				marketUserProduct.setProductOrginPrice(Double.parseDouble(productJd.getOriPrice()));
                                				marketUserProduct.setProductHongbao(Double.parseDouble(productJd.getConTicketPrice()));
                                				marketUserProduct.setProductPrice(Double.parseDouble(productJd.getConPrice()));
                                				marketUserProduct.setMarketProfit(productJd.getCommissionProfit());
                                				marketUserProduct.setMarketProfitPercent(productJd.getCommission());
                                				marketUserProduct.setTotalSales(Integer.parseInt(RandomStringHelper.randomNumStr(3)));
                                				marketUserProduct.setMainImageUrl(productJd.getGoodsUrl());
                                				marketUserProduct.setProfit("0");
                                				marketUserProduct.setProductType("002");
                        						userProducts.add(marketUserProduct);
                        						String qcUrl = getConUrl(getTgwidByUserid(user.getId()), productJd.getSku(), productJd.getConUrlB());
                        						String imageUrl = getPoster(productJd.getGoodsName(), productJd.getGoodsUrl(), productJd.getOriPrice(), productJd.getConPrice(), productJd.getConTicketPrice() , productJd.getTotalSales(), "jd", qcUrl);
                        						list.add( imageUrl );
                        						//list.add( url + productJd.getId() + "&marketUserProductId="+ marketUserProduct.getId() + "&yunId=" + user.getId() ); 
                        					}
                						}
                						
                					}
                				}
            				}
        				}
        				
        				batch.setImageUrls(list);
        				batch.setWord(word);
        				
        				result = marketUserProductService.addBatch(userProducts);
        				resultMap.put(DATA,batch);
        				errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG;
    				}else{
    					result = FAIL;
        				errorCode = WEIXIN_BATCH_ERROR_CODE;
        	        	errorMsg = WEIXIN_BATCH_ERROR_MSG; 
    				}
    				
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    			
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    /**
     * 立刻推广多件
     * @return
     */
    @RequestMapping(value="/multiSend")
    @ResponseBody
    public Map<String,Object> multiSend( HttpServletRequest request,HttpServletResponse response, String token ,String ids ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(ids) ){
    			MarketUser user = getMarketUserByToken(token);
    			//验证用户TOKEN
    			if( null != user ){
    				String[] split = ids.split(",");
    				//List<WeixinVo> list = new ArrayList<WeixinVo>();
    				List<String> list = new ArrayList<String>();
    				String word="";
    				List<MarketUserProduct> userProducts = new ArrayList<MarketUserProduct>();
    				WeixinVo batch = new WeixinVo();
    				for( int i=0; i<split.length; i++ ){
    					Product product = productService.get(split[i]);
    					if( null != product ){
    						//WeixinVo batch = new WeixinVo();
        					//判断用户的商品有没有，有就返回存在的
        					MarketUserProductExample example = new MarketUserProductExample();
        					Criteria criteria = example.createCriteria();
        					criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        					criteria.andUserIdEqualTo(user.getId());
        					criteria.andProductIdEqualTo(split[i]);
        					List<MarketUserProduct> ttmp = marketUserProductService.list(example);
        					String url = "http://file.jhjvip.cn:8082/mall/mobile/image/mergeYun?spuId=";
        					if( ttmp.size() > 0 ){
        						
        						MarketUserProduct marketUserProduct = ttmp.get(0);
        						list.add( url + product.getId() + "&marketUserProductId="+ marketUserProduct.getId()+"&yunId="+user.getId() );
        						//batch.setImageUrl(product.getMainImageUrl());
        						//batch.setWord(getWordByMarketUserId(marketUserProduct.getProductId(),marketUserProduct.getId()));
        						//word += getWord(product.getName(),product.getProductHongbao(),product.getProductPrice(),marketUserProduct.getProductId(),marketUserProduct.getId());
        					}else{
        						String marketUserProductId = IdGen.uuid();
        						
            					MarketUserProduct marketUserProduct = new MarketUserProduct();
            					marketUserProduct.setId(marketUserProductId);
            					marketUserProduct.setOrgCode(ORG_CODE);
            					marketUserProduct.setCreateTime(new Date());
            					marketUserProduct.setUpdateTime(new Date());
            					marketUserProduct.setIsEffective(DBConst.TRUE);
            					marketUserProduct.setCreateUser(CREATE_USER);
                				//业务字段
                				marketUserProduct.setUserId(user.getId());
                				marketUserProduct.setProductId(product.getId());
                				marketUserProduct.setCategoryId(product.getCategoryId());
                				marketUserProduct.setName(product.getName());
                				marketUserProduct.setProductOrginPrice(Double.parseDouble(product.getProductOrginPrice()+""));
                				marketUserProduct.setProductHongbao(getHongbaoDouble(product));
                				marketUserProduct.setProductPrice(Double.parseDouble(getProdcutPrice(product)));
                				marketUserProduct.setMarketProfit(product.getMarketProfit());
                				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
                				marketUserProduct.setTotalSales(product.getTotalSales());
                				marketUserProduct.setMainImageUrl(product.getMainImageUrl());
                				marketUserProduct.setProfit("0");
                				marketUserProduct.setMarketProfit(product.getMarketProfit());
                				marketUserProduct.setMarketProfitPercent(product.getMarketProfitPercent());
                				marketUserProduct.setProductType("001");
                				//batch.setImageUrl(product.getMainImageUrl());
        						//batch.setWord(getWordByMarketUserId(marketUserProduct.getProductId(),marketUserProduct.getId()));
                				//list.add(product.getMainImageUrl());
                				//word += getWord(product.getName(),product.getProductHongbao(),product.getProductPrice(),marketUserProduct.getProductId(),marketUserProduct.getId());
        						userProducts.add(marketUserProduct);
        						list.add( url + product.getId() + "&marketUserProductId="+ marketUserProduct.getId() + "&yunId=" + user.getId() );
        					}
    					}
    					
        				//list.add(batch); 
    				}
    				batch.setImageUrls(list);
    				batch.setWord(word);
    				
    				result = marketUserProductService.addBatch(userProducts);
    				resultMap.put(DATA,batch);
    				errorCode = 	SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	e.printStackTrace();
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    public String getWord( String name,String hongbao,String price,String productId,String marketUserProductId ){
    	StringBuffer buffer = new StringBuffer();
    	//buffer.append("名称：");
    	buffer.append(name);
    	buffer.append("\n");
    	buffer.append("红包：");
    	buffer.append(hongbao);
    	buffer.append("元");
    	buffer.append("\n");
    	buffer.append("优惠价：");
    	buffer.append(price);
    	buffer.append("元");
    	buffer.append("\n");
    	buffer.append("链接地址：");
    	buffer.append(getMarketUserCommonUrl());
    	buffer.append("?productId=");
    	buffer.append(productId);
    	buffer.append("&marketUserProductId=");
    	buffer.append(marketUserProductId);
    	buffer.append("\n\n");
    	return buffer.toString();
    }
    
    
    public String getWordByMarketUserId( String productId,String marketUserProductId,String yunId ){
    	return getMarketUserCommonUrl() + "?productId=" + productId + "&marketUserProductId=" + marketUserProductId + "&yunId="+yunId;
    }
    
    
    
    /**
     * 根据内容，生成指定宽高、指定格式的二维码图片
     *
     * @param text   内容
     * @param width  宽
     * @param height 高
     * @param format 图片格式
     * @return 生成的二维码图片路径
     * @throws Exception
     */
    @RequestMapping(value="/generateQRCode")
    @ResponseBody
    private String generateQRCode(String text, int width, int height, String format) throws Exception {
        return QcodeUtils.create(text,width,height,format);
    }
    
}